<?php
 	if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
 
 	class MY_Controller extends CI_Controller{
 		private $privilege_list;
 		public $offset = 20;
 		public $jump_url;
   		function __construct() {
    		parent::__construct();
    		$this->jump_url = !empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/main';
			$this->load->model('privileges');
			$this->privilege_list = $this->privileges->get_list(1);
			if( ! isset($this->session->userdata['manage_user'])){
				show_message('/login', "您尚未登陆，或者登陆已失效！");exit;
			}else{
				$request_url_arr = explode('/', $_SERVER['REQUEST_URI']);
				if(isset($request_url_arr[1]) && isset($request_url_arr[2])){
					$request_url = $request_url_arr[1] . '/' . $request_url_arr[2];
				}elseif(isset($request_url_arr[1]) && ! isset($request_url_arr[2])){
					$request_url = $request_url_arr[1];
				}
				$request_url = str_replace("/edit", "/index", $request_url);
				$request_url = str_replace("/add", "/index", $request_url);
				$controller_now = get_field_by_where("sys_privileges", "id, pid", "menu_url = '{$request_url}'");
				$this->smarty->assign("pid", isset($controller_now['pid']) ? $controller_now['pid'] : 0);
				$this->smarty->assign("iid", isset($controller_now['id']) ? $controller_now['id'] : 0);
				$manage_user = $this->session->userdata['manage_user'];
				$this->smarty->assign("manage_user_name", $manage_user['username']);
				if($manage_user['username'] != 'admin' && $this->uri->segment(1) != 'upload'){
					$this->check_user_privilege($manage_user);
					/*$this->load->model('roles');
					$manage_privilege = $this->roles->get_role_info($manage_user['role_id']);
					if(isset($manage_privilege['role_privilege']) && $manage_privilege['role_privilege'] != ''){
						$role_privilege = json_decode($manage_privilege['role_privilege']);
					}*/
				}else{
					$this->smarty->assign("menu_list", $this->privilege_list);
				}
			}
			// print_r($this->privilege_list);
			
		}

		function check_user_privilege($manage_user){
			$this->load->model("roles");
			$role_info = $this->roles->get_role_info($manage_user['role_id']);
			if($role_info['role_privilege'] == ''){
				show_message('/login', '您没有权限进入管理后台，请先联系超级管理员分配权限');
				exit;
			}else{
				$role_privilege = json_decode($role_info['role_privilege']);
				$manage_privilege = array();
				foreach ($this->privilege_list as $key => $value) {
					$sub = array();
					if(isset($value->sub) && count($value->sub) > 0){
						foreach ($value->sub as $k => $val) {
							if(in_array($val->menu_url, $role_privilege)){
								$manage_privilege[$val->pid] = (object)array('id' => $value->id, 'pid' => $value->pid, 'action' => $value->action, 'menu_title' => $value->menu_title, 'menu_url' => $value->menu_url, 'sort' => $value->sort, 'add_time' => $value->add_time, 'status'=>$value->status);
								$sub[] = $val;
								
							}
						}
						if(count($sub) > 0){
							$manage_privilege[$val->pid]->sub = $sub;
						}
					}
				}
				$manage_privilege = (object) $manage_privilege;
				$this->smarty->assign("menu_list", $manage_privilege);
				$url_total = $this->uri->total_segments();
				if($url_total >= 2){
					$user_privilege = $this->uri->segment(1) . '/' . $this->uri->segment(2);
				}else{
					$user_privilege = $this->uri->segment(1) . '/index';
				}
				$cls_methods = get_class_methods($this->uri->rsegment(1));
				if( current(explode("_", $this->uri->rsegment(2))) . '_' != 'ignore_' && in_array($this->uri->rsegment(2), $cls_methods) && ! in_array($user_privilege, $role_privilege ) && $this->uri->segment(2) != 'save' && $this->uri->segment(2) != 'get_qiniu_token'){
					if(is_ajax_request()){
						echo json_encode(array('code' => 9, 'msg' => '您没有权限进行该操作！'));
						exit;
					}else{
						if(count($role_privilege) > 0){
							show_message('/'.$role_privilege[0], '您没有权限进行该操作！');	
						}else{
							show_message('/manage_user/ignore_update_manage_user_password', '您当前没有任何权限！');
						}
						exit;
					}
				}
			}
		}
	}